/**
 *
 * @author DorcasBless
 * you can find the Synaxarium at http://code.google.com/p/synaxarium/
 * April, 2009
 * Version 1.0.0
 *
 */

package com.christian;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Vector;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class NameSearchForm extends Form implements CommandListener {

    private SynMIDlet synMIDlet;
    private ChoiceGroup resultChoice = new ChoiceGroup(null, ChoiceGroup.EXCLUSIVE);
    private TextField searchName = new TextField("\u0646\u0635 \u0627\u0644\u0628\u062D\u062B:", "", 60, TextField.ANY);
    private Command searchCommand = new Command("\u0628\u062D\u062B", Command.OK, 0);
    private Command okCommand = new Command("\u0645\u0648\u0627\u0641\u0642", Command.OK, 0);
    private Command backCommand = new Command("\u0631\u062C\u0648\u0639", Command.BACK, 0);
    private Vector pageIndex = new Vector();
    private char[] strArr;
    private Vector strVector;
    private String searchNa;

    public NameSearchForm(SynMIDlet synMIDlet) {
        super("\u0628\u062D\u062B");
        this.synMIDlet = synMIDlet;
        if (Constatnts.MIDP20)
        {
            resultChoice.setFitPolicy(ChoiceGroup.TEXT_WRAP_ON);
        }
        append(searchName);
        addCommand(searchCommand);
        append(resultChoice);
        addCommand(okCommand);
        addCommand(backCommand);
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == searchCommand) {

            DataInputStream out2 = new DataInputStream(synMIDlet.getClass().getResourceAsStream("/1/index"));
            int cout = 1;
            String temp = null;
            resultChoice.deleteAll();
            
            searchNa = searchName.getString();
            int searchIndex = searchNa.indexOf("\u064A", 0);
            int searchIndexSpace = searchNa.indexOf("\u064A ", 0);
            strArr = searchNa.toCharArray();
            strVector = new Vector();
            strVector.addElement(searchNa);

            // Yaa2 ي
            while (searchIndex == (searchNa.length() - 1) || searchIndexSpace > -1) {
                strArr[searchIndex] = '\u0649';
                strVector.addElement(new String(strArr));
                searchIndex = searchNa.indexOf("\u064A", searchIndex + 1);
                searchIndexSpace = searchNa.indexOf("\u064A ", searchIndex + 1);
            }

            // waw ؤ
            searchIndex = searchNa.indexOf("\u0648", 0);
            while (searchIndex == (searchNa.length() - 1) || searchIndex > -1) {
                strArr[searchIndex] = '\u0624';
                strVector.addElement(new String(strArr));
                searchIndex = searchNa.indexOf("\u0648", searchIndex + 1);
            }

            //Alf ا
            seacrchAlef ('\u0627','\u0623','\u0625','\u0622');
            //Alf أ
            seacrchAlef ('\u0623','\u0627','\u0625','\u0622');
            //Alf إ
            seacrchAlef ('\u0625','\u0627','\u0623','\u0622');
            //Alf آ
            seacrchAlef ('\u0622','\u0627','\u0623','\u0625');

            strArr = null;
            int vectorCount = 0;
            int vectorSize = strVector.size();
            pageIndex.setSize(0);
            resultChoice.setLabel("\u0646\u062A\u064A\u062C\u0629 \u0627\u0644\u0628\u062D\u062B :");
            while (cout < 796) {
                try {
                    temp = out2.readUTF();
                    while (vectorCount < vectorSize) {
                        searchIndex = temp.indexOf((String) strVector.elementAt(vectorCount));
                        if (searchIndex != -1) {
                            getResultChoice().append(temp.substring(0, temp.indexOf(' ')) + " " + temp.substring(searchIndex, temp.lastIndexOf(' ')), null);
                            pageIndex.addElement(temp.substring(temp.lastIndexOf(' ') + 1));
                        }
                        vectorCount++;
                    }
                    vectorCount = 0;
                    cout++;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
            if (resultChoice.size() == 0) {
                resultChoice.setLabel("\u0644\u0627 \u064A\u0648\u062C\u062F \u0646\u062A\u0627\u0626\u062C \u0644\u0644\u0628\u062D\u062B");
            }

            try {
                out2.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else if (c == okCommand) {
            int i = resultChoice.getSelectedIndex();
            String test = resultChoice.getString(i);
            int dateIndex = test.indexOf('/');
            SynMIDlet.calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(test.substring(dateIndex + 1, test.indexOf(' '))));
            SynMIDlet.calendar.set(Calendar.MONTH, Integer.parseInt(test.substring(0, dateIndex)) - 1);
            if (synMIDlet.getCanvas().init("/" + test.substring(0, test.indexOf(' ')) + ".txt"))
            {
                synMIDlet.getCanvas().setCurrentPageNo(Integer.parseInt((String) pageIndex.elementAt(i)));
                synMIDlet.showNewMainScreen();
            }
        } else if (c == backCommand) {
            //synMIDlet.showSameMainScreen();
            synMIDlet.showSameMainScreen();
        }
    }

    /**
     * @return the resultChoice
     */
    public ChoiceGroup getResultChoice() {
        return resultChoice;
    }

    /**
     * @param resultChoice the resultChoice to set
     */
    public void setResultChoice(ChoiceGroup resultChoice) {
        this.resultChoice = resultChoice;
    }

    private void seacrchAlef (char main,char alt1,char alt2,char alt3)
    {
        int searchIndex = searchNa.indexOf(main, 0);
            while (searchIndex > -1) {
                strArr = searchNa.toCharArray();

                // Alf Hamza fo2 ا
                strArr[searchIndex] = alt1;
                strVector.addElement(new String(strArr));

                // Alf Hamza ta7t إ
                strArr[searchIndex] = alt2;
                strVector.addElement(new String(strArr));

                // Alf Hamza ta7t آ
                strArr[searchIndex] = alt3;
                strVector.addElement(new String(strArr));

                searchIndex = searchNa.indexOf(" "+main, searchIndex + 1);
            }
    }
}
